package com.gmail.blackdog1987.carcare.util;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.multipart.commons.CommonsMultipartFile;

/**
 * 图片上传工具类
 * 
 * @author liww
 * @date 2014-8-14
 */
public class ImageUploadUtil {
	public static final String uploadRootDir = "/root";
	
	public static final String uploadCtxDirPattern = "/usr/update/img/${yyyy}/${MM}/${dd}/";

	public static Map<String, Object> imageUpload(CommonsMultipartFile[] images) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("code", ErrorCode.NO);
		if (images != null && images.length > 0) {
			List<String> relativePathList = new ArrayList<String>();
			for (int i = 0; i < images.length; i++) {
				CommonsMultipartFile image = images[i];
				if (image != null
						&& !NullUtil.isEmpty(image.getOriginalFilename())) {
					String fileName = FileNameUtils.genFileName();
					String fileExt = FileNameUtils.getExtension(image
							.getFileItem().getName());
					// 图片
					String uploadCtxDir = "";
					if(fileExt==null||fileExt.length()==0){
						fileExt = "jpg";
					}
//					System.err.println(fileExt);
//					if ("jpg".equals(fileExt) || "png".equals(fileExt)
//							|| "gif".equals(fileExt)) {
						uploadCtxDir = StrUtils.replaceParams(uploadCtxDirPattern,
								getTime());
						String path = uploadRootDir + uploadCtxDir;
						path = StrUtils.replaceParams(path, getTime());

						File filePath = new File(path);
						if (!filePath.exists()) {
							filePath.mkdirs();
						}

						File destFile = new File(path,
								FileNameUtils.genFileName(fileName, fileExt)); // 创建原始文件
						try {
							image.transferTo(destFile);
						} catch (IllegalStateException e) {
							result.put("code", ErrorCode.UNKNOWN);
							result.put("msg", ErrorCodeMsg.UNKNOWN);
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
							result.put("code", ErrorCode.UNKNOWN);
							result.put("msg", ErrorCodeMsg.UNKNOWN);
						}
						// 获取大图路径
						String relativePath = uploadCtxDir + fileName + "."
								+ fileExt;
						relativePathList.add(relativePath);
//					} else {
//						result.put("code", ErrorCode.IMG_FORMAT_ERROR);
//						result.put("msg", ErrorCodeMsg.IMG_FORMAT_ERROR);
//						return result;
//					}
				}
			}
			result.put("relativePath", relativePathList);
		}
		return result;
	}

	/**
	 * 获取时间变量
	 * 
	 * @param siteConf
	 * @return
	 */
	protected static Map<String, String> getTime() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String dateStr = format.format(new Date());
		String[] dateStrs = dateStr.split("-");
		Map<String, String> params = new HashMap<String, String>();
		params.put("yyyy", dateStrs[0]);
		params.put("MM", dateStrs[1]);
		params.put("dd", dateStrs[2]);
		return params;
	}
}
